﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;

using PowerCreator.LMS.Common.Script;
using PowerCreator.LMS.Web.User;
using PowerCreator.LMS.Web.Task;
using PowerCreator.LMS.Task.Entity;
using PowerCreator.LMS.ViewQuery;
using PowerCreator.LMS.Common.Utils;

public partial class sysadmin_TaskManager_EditTask : System.Web.UI.Page
{
    protected PowerCreator.LMS.Web.Config.WebConfig config = new PowerCreator.LMS.Web.Config.WebConfig();
    TaskClassRoomManag objTaskClassRoomManag = new TaskClassRoomManag();
    EvaluateManag objEvaluateManag = new EvaluateManag();
    TaskManag objTaskManag = new TaskManag();
    User objUser = new User();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            if (Request.Params["id"] != null && Request.Params["id"].Trim() != "")
            {
                string id = Request.Params["id"];
                ShowInfo(int.Parse(id));
            }
        }
    }

    private void ShowInfo(int TaskID)
	{
		VOTask model = new VOTask();
        model = objTaskManag.Load(TaskID);
        //this.lblTaskID.Text=model.TaskID.ToString();
		this.txtTaskName.Text=model.TaskName;
        //this.txtTaskTrainingDays.Text=model.TaskTrainingDays.ToString();
		this.txtTaskStartTime.Text=model.TaskStartTime.ToString();
		this.txtTaskEndTime.Text=model.TaskEndTime.ToString();
        //this.txtTaskTimeType.Text=model.TaskTimeType.ToString();
        //this.txtTaskUserCount.Text=model.TaskUserCount.ToString();
        //this.txtTaskAuditState.Text=model.TaskAuditState.ToString();
        //this.txtTaskAppraiseType.Text=model.TaskAppraiseType.ToString();
        //this.txtFKTaskEvaluateID.Text=model.FKTaskEvaluateID.ToString();
        //this.txtFKTaskResourseID.Text=model.FKTaskResourseID;
		this.txtTaskRemark.Text=model.TaskRemark;
        //this.txtFKTaskTeacherID.Text=model.FKTaskTeacherID.ToString();
        //this.txtFKTaskClassRoomID.Text=model.FKTaskClassRoomID.ToString();
        //this.txtTaskCreateTime.Text=model.TaskCreateTime.ToString();
        //this.txtFKTaskCreateID.Text=model.FKTaskCreateID.ToString();

        DataTable dtTaskClassRoom = objTaskClassRoomManag.Query("");
        dpdlsTaskClassRoomt.DataSource = dtTaskClassRoom;
        dpdlsTaskClassRoomt.DataTextField = "TaskCrName";
        dpdlsTaskClassRoomt.DataValueField = "TaskCrID";
        dpdlsTaskClassRoomt.DataBind();

        for (int i = 0; i < dpdlsTaskClassRoomt.Items.Count; i++)
        {
            if (dpdlsTaskClassRoomt.Items[i].Value == model.FKTaskClassRoomID.ToString())
            {
                dpdlsTaskClassRoomt.Items[i].Selected = true;
                return;
            }
        }


        if (dpdlstTaskAppraiseType.SelectedValue == "2")
        {
            dpdlstTaskEvaluateName.Visible = true;
        }

        DataTable dtTaskEvaluate = objEvaluateManag.Query("");
        dpdlstTaskEvaluateName.DataSource = dtTaskEvaluate;
        dpdlstTaskEvaluateName.DataTextField = "EvateName";
        dpdlstTaskEvaluateName.DataValueField = "EvateID";
        dpdlstTaskEvaluateName.DataBind();

        for (int i = 0; i < dpdlstTaskEvaluateName.Items.Count; i++)
        {
            if (dpdlstTaskEvaluateName.Items[i].Value == model.FKTaskEvaluateID.ToString())
            {
                dpdlstTaskEvaluateName.Items[i].Selected = true;
                return;
            }
        }

        DataTable dtTaskTeacher = objUser.QueryTeacher("", int.MaxValue, int.MaxValue);
        dpdlstTaskTeacherID.DataSource = dtTaskTeacher;
        dpdlstTaskTeacherID.DataTextField = "TeacherName";
        dpdlstTaskTeacherID.DataValueField = "FKTeacherUserId";
        dpdlstTaskTeacherID.DataBind();

        for (int i = 0; i < dpdlstTaskTeacherID.Items.Count; i++)
        {
            if (dpdlstTaskTeacherID.Items[i].Value == model.FKTaskTeacherID.ToString())
            {
                dpdlstTaskTeacherID.Items[i].Selected = true;
                return;
            }
        }

	}
    protected void btnSave_Click(object sender, EventArgs e)
    {
        string strErr = "";
        if (this.txtTaskName.Text == "")
        {
            strErr += "TaskName不能为空！\\n";
        }
        if (!TimeUtils.IsDateTime(txtTaskStartTime.Text))
        {
            strErr += "TaskStartTime不是时间格式！\\n";
        }
        if (!TimeUtils.IsDateTime(txtTaskEndTime.Text))
        {
            strErr += "TaskEndTime不是时间格式！\\n";
        }
    	if(strErr!="")
		{
            Script.Alert(this, "msg", strErr);
            return;
		}
        string TaskName = this.txtTaskName.Text;
        //int TaskTrainingDays = 0;
        DateTime TaskStartTime = DateTime.Parse(this.txtTaskStartTime.Text);
        DateTime TaskEndTime = DateTime.Parse(this.txtTaskEndTime.Text);
        //int TaskTimeType = 1;
        //int TaskUserCount = 0;
        int TaskAuditState = 0;
        int TaskAppraiseType = int.Parse(dpdlstTaskAppraiseType.SelectedValue);
        int FKTaskEvaluateID = int.Parse(dpdlstTaskEvaluateName.SelectedValue);
        //string FKTaskResourseID = "";
        string TaskRemark = this.txtTaskRemark.Text;
        int FKTaskTeacherID = int.Parse(dpdlstTaskTeacherID.SelectedValue);
        int FKTaskClassRoomID = int.Parse(dpdlsTaskClassRoomt.SelectedValue);
        DateTime TaskCreateTime = DateTime.Now;
        //int FKTaskCreateID = 10000;


        VOTask model = new VOTask();
		model.TaskName=TaskName;
        //model.TaskTrainingDays=TaskTrainingDays;
		model.TaskStartTime=TaskStartTime;
		model.TaskEndTime=TaskEndTime;
        //model.TaskTimeType=TaskTimeType;
        //model.TaskUserCount=TaskUserCount;
		model.TaskAuditState=TaskAuditState;
		model.TaskAppraiseType=TaskAppraiseType;
		model.FKTaskEvaluateID=FKTaskEvaluateID;
        //model.FKTaskResourseID=FKTaskResourseID;
		model.TaskRemark=TaskRemark;
		model.FKTaskTeacherID=FKTaskTeacherID;
		model.FKTaskClassRoomID=FKTaskClassRoomID;
        //model.TaskCreateTime=TaskCreateTime;
        //model.FKTaskCreateID=FKTaskCreateID;
        objTaskManag.Update(model);
        Script.RedirectURL("TaskManager.aspx");
    }
    protected void btnReset_Click(object sender, EventArgs e)
    {
        Script.RedirectURL("TaskManager.aspx");
    }
    protected void dpdlstTaskAppraiseType_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (dpdlstTaskAppraiseType.SelectedValue == "2")
        {
            dpdlstTaskEvaluateName.Visible = true;
        }
        else
        {
            dpdlstTaskEvaluateName.Visible = false;
        }
    }
}
